// home/pages/place_an_order/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ids:[],
    all_weight:0,
    product_type_id:0,
    total_value:'',
    address_id:'',
    remark:'',
    template_id:'',
    product_show:false,
    show:false,
    length:'',
    width:'',
    height:'',
    address_show:false,
    list:[],
    loading:false,
    cloum_status:false,
    shipping_fee:'0.00',
    o_index:0,
    colum_list:[],
    details_show:false,
    theme_color:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    if(wx.getStorageSync('place_order')){
      let arr=wx.getStorageSync('place_order')
      let num=0;
      for(let i in arr){
        num+=arr[i].weight*1
      }
      this.setData({
        ids:arr,
        all_weight:num
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      theme_color:app.dlc.gain('theme_color')
    })
  },
  go_back(){
    wx.navigateBack({
      delta:1
    })
  },
  show_product(){
    this.setData({
      product_show:true
    })
  },
  productClose(){
    this.setData({
      product_show:false
    })
  },
  show_demo(){
    this.setData({
      show:true
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  get_index(e){
    let index=e.currentTarget.dataset.index
    this.setData({
      product_type_id:index,
      show:false
    })
  },
  get_length(e){
    this.setData({
      length:e.detail.value
    })
  },
  get_width(e){
    this.setData({
      width:e.detail.value
    })
  },
  get_height(e){
    this.setData({
      height:e.detail.value
    })
  },
  get_num(e){
    this.setData({
      total_value:e.detail.value
    })
  },
  get_textarea(e){
    this.setData({
      remark:e.detail.value
    })
  },
  addressClose(){
    this.setData({
      address_show:false
    })
  },
  show_address(){
    this.setData({
      address_show:true
    })
    this.get_address()
  },
  get_address() {
    const that = this;
    that.setData({
      loading: true
    })
    app.wxRequest('/api/Address/index', {
      page: 1,
      type: that.data.active
    }, function (res) {
      if (res.code == 200) {
        let list = res.data;
        that.setData({
          list: list,
          loading:false
        })
      } else {
        app.toast(res.message)
      }
    })
  },
  choose_address(e){
    let index=e.currentTarget.dataset.index;
    let list=this.data.list;
    this.setData({
      address_id:list[index].id,
      address:list[index].recipient_name+list[index].recipient_phone,
      address_show:false,
      country_code:list[index].recipient_country
    })
    this.cloum_data()
  },
  confirm_product(){
    this.productClose()
    this.cloum_data()
  },
  confirm_order(){
    const that=this;
    if(app.dlc.isNull(this.data.ids)){
      app.toast('包裹不存在')
    }else if(this.data.product_type_id==0){
      app.toast('请选择是否是敏感物品')
    }else if(!this.data.total_value){
      app.toast('请填写总价')
    }else if(!this.data.remark){
      app.toast('请输入备注信息')
    }else if(!this.data.address_id){
      app.toast('请选择收货地址')
    }else{
      app.loading()
      let ids=this.data.ids.map(item=>{
        return item.id
      })
      app.wxRequest('/api/Goods/createPack',{
        ids:ids.join(','),
        product_type_id:that.data.product_type_id,
        total_value:that.data.total_value,
        address_id:that.data.address_id,
        template_id:that.data.template_id,
        remark:that.data.remark
      },function(res){
        if(res.code==200){
          app.toast('下单成功')
          wx.setStorageSync('packge', 1)
          wx.switchTab({
            url:'/pages/order/index'
          })
        }else{
          app.toast(res.msg)
        }
      })
    }
  },
  cloum_data(){
    const that=this;
    if(app.dlc.isNull(that.data.all_weight)){
      app.toast('请选择物品')
    }else if(app.dlc.isNull(that.data.length)){
      app.toast('请填写物品长度')
    }else if(app.dlc.isNull(that.data.width)){
      app.toast('请填写物品宽度')
    }else if(app.dlc.isNull(that.data.height)){
      app.toast('请填写物品高度')
    }else if(app.dlc.isNull(that.data.product_type_id)){
      app.toast('请选择物品是否是敏感物')
    }else if(!that.data.country_code){
      app.toast('请选择物收货地址')
    }else{
      that.cloum_price()
    }
  },
  cloum_price(){
      const that=this;
      app.wxRequest('/api/Pack/calculateShippingCost',{
        country:that.data.country_code,
        weight:that.data.all_weight,
        length:that.data.length,
        width:that.data.width,
        height:that.data.height,
        goods_type_id:that.data.product_type_id,
      },function(res){
        if(res.code==200){
          if(res.data){
            that.setData({
              cloum_status:true,
              colum_list:res.data,
              shipping_fee:res.data[that.data.o_index].totalShippingCost,
              template_id:res.data[that.data.o_index].id
            })
          }else{
            app.toast('未查询到运费信息')
          }
        }else{
          app.toast(res.message)
        }
      })
  },
  get_cloum(e){
    let index=e.currentTarget.dataset.index;
      this.setData({
          o_index:index,
          shipping_fee:this.data.colum_list[index].totalShippingCost,
          template_id:this.data.colum_list[index].id
      })
  },
  show_top(){
    if(this.data.cloum_status){
      this.setData({
        details_show:true
      })
    }
  },
  close_top(){
    this.setData({
      details_show:false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})